#!/bin/env python
import os
import sys
import string

def install_boost(src,addr,extra_options=''):
    sys.stderr.write('------- INSTALLING BOOST LIBRARY ------------\n')
    try:
        with open(src) as f: pass
    except IOError as e:
        print "Downloading %s" % (src)
        os.system('wget %s/%s' % (addr,src) )
    root = os.getenv('ROOTPATH')
    directory = os.path.splitext(os.path.splitext( src )[0])[0]
    os.system('tar xzf %s' % (src))
    os.system('cd %s; ./bootstrap.sh --prefix=%s --libdir=%s/lib %s; ./b2 install' % (directory,root,root,extra_options))
    os.system('rm -rf %s' % (directory))
    sys.stderr.write('------- END OF BOOST LIBRARY INSTALLATION------------\n')


def install_package(src,addr,extra_options=''):
    sys.stderr.write('------- INSTALLING %s ------------\n' % (src))
    try:
        with open(src) as f: pass
    except IOError as e:
        print "Downloading %s" % (src)
        os.system('wget %s/%s' % (addr,src) )
    root = os.getenv('ROOTPATH')
    directory = os.path.splitext(os.path.splitext( src )[0])[0]
    os.system('tar xzf %s' % (src))
    os.system('cd %s; ./configure --prefix=%s --libdir=%s/lib %s; make; make install' % (directory,root,root,extra_options))
    os.system('rm -rf %s' % (directory))
    sys.stderr.write('------- END OF  %s INSTALLATION ------------\n' % (src))


print "----- Installing basic environment ------"

tmpdir = 'ctmp' 
try:
    os.mkdir(tmpdir)
except OSError as e:
    print "Reusing existing tmp directory"
retval = os.getcwd()
os.chdir(tmpdir)

rootpath = os.getenv('ROOTPATH')
os.environ['CPPFLAGS'] = '-I%s/include' % (rootpath)
os.environ['LDFLAGS'] = '-I%s/lib' % (rootpath)

packages = map(string.strip,open('../install.cfg').readlines())

if 'NETCDF' in packages:
    install_package('netcdf-4.1.3.tar.gz','http://www.unidata.ucar.edu/downloads/netcdf/ftp','--disable-fortran --disable-netcdf-4')
#   install_package('netcdf-cxx4-4.2.tar.gz','http://www.unidata.ucar.edu/downloads/netcdf/ftp')
if 'BOOST'  in packages:
    install_boost('boost_1_52_0.tar.gz','http://sourceforge.net/projects/boost/files/boost/1.52.0')
if 'FFTW' in packages:
    install_package('fftw-3.3.2.tar.gz','http://www.fftw.org','--enable-float --enable-shared')
    install_package('fftw-3.3.2.tar.gz','http://www.fftw.org','--enable-shared')
    install_package('fftw-3.3.2.tar.gz','http://www.fftw.org','--enable-long-double --enable-shared')
if 'SWIG' in packages:
    install_package('pcre-8.31.tar.gz','ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre')
    install_package('swig-2.0.8.tar.gz','http://prdownloads.sourceforge.net/swig')
if 'FRAMELIB' in packages:
    install_package('libframe-8.17.2.tar.gz','http://lappweb.in2p3.fr/virgo/FrameL')
if 'GSL' in packages:
    install_package('gsl-1.15.tar.gz','http://mirror2.mirror.garr.it/mirrors/gnuftp/gnu/gsl')
if 'R' in packages:
    install_package('R-2.15.2.tar.gz','http://cran.r-project.org/src/base/R-2','--without-x --without-readline')


os.chdir(retval)
os.system('rm -rf %s' % (tmpdir))
#
